package com.neu.question.controller;

import com.neu.question.beans.HttpResponseEntity;
import com.neu.question.common.Constans;
import com.neu.question.service.QuestionInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

//问题类
@RestController
public class QuestionController {
    private final Logger logger = LoggerFactory.getLogger(QuestionController.class);

    @Autowired
    private QuestionInfoService questionInfoService;

    /**
     * 添加题目
     */
    @RequestMapping(value = "/addQuestion", method = RequestMethod.POST,
            headers = "Accept=application/json")
    public HttpResponseEntity addQuestionnaire(@RequestBody Map<String, Object> map) {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        int status = questionInfoService.addQuestion(map);
        try {
            if (status == 1) {
                httpResponseEntity.setCode(Constans.SUCCESS_CODE);
                httpResponseEntity.setMessage(Constans.ADD_MESSAGE);
            } else if (status == 0) {
                httpResponseEntity.setCode(Constans.EXIST_CODE);
                httpResponseEntity.setMessage(Constans.NAME_EXIT_MESSAGE);
            } else {
                httpResponseEntity.setCode(Constans.EXIST_CODE);
                httpResponseEntity.setMessage(Constans.COPY_EXIT_UPDATE_MESSAGE);
            }
        } catch (Exception e) {
            logger.info("addQuestionnaireInfo 添加问卷>>>>>>>>>>>" + e.getLocalizedMessage());
            httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
            httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
        }
        return httpResponseEntity;
    }

    /**
     * 根据题目标题查询题目
     */
    @RequestMapping(value = "/getQuestionByTitle", method = RequestMethod.POST,
            headers = "Accept=application/json")
    public HttpResponseEntity getQuestionByTitle(@RequestBody Map<String, Object> map) {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try {
            httpResponseEntity.setCode(Constans.SUCCESS_CODE);
            httpResponseEntity.setMessage(Constans.STATUS_MESSAGE);
            httpResponseEntity.setData(questionInfoService.getQuestionByTitle(map));
        } catch (Exception e) {
            logger.info("getQuestionByTitle 根据题目标题查询题目>>>>>>>>>>>" + e.getLocalizedMessage());
            httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
            httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
        }
        return httpResponseEntity;
    }

    /**
     * 查询所有题目
     */
    @RequestMapping(value = "/getAllQuestion", method = RequestMethod.POST,
            headers = "Accept=application/json")
    public HttpResponseEntity getAllQuestion(@RequestBody Map<String, Object> map) {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try {
            httpResponseEntity.setCode(Constans.SUCCESS_CODE);
            httpResponseEntity.setMessage(Constans.STATUS_MESSAGE);
            httpResponseEntity.setData(questionInfoService.getAllQuestion(map));
        } catch (Exception e) {
            logger.info("getAllQuestion 查询所有题目>>>>>>>>>>>" + e.getLocalizedMessage());
            httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
            httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
        }
        return httpResponseEntity;
    }

    /**
     * 编辑题目
     */
    @RequestMapping(value = "/updateQuestion", method = RequestMethod.POST,
            headers = "Accept=application/json")
    public HttpResponseEntity updateQuestion(@RequestBody Map<String, Object> map) {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        int status = questionInfoService.updateQuestion(map);
        try {
            if (status == 1) {
                httpResponseEntity.setCode(Constans.SUCCESS_CODE);
                httpResponseEntity.setMessage(Constans.UPDATE_MESSAGE);
            } else if (status == 0) {
                httpResponseEntity.setCode(Constans.EXIST_CODE);
                httpResponseEntity.setMessage(Constans.NAME_EXIT_MESSAGE);
            } else {
                httpResponseEntity.setCode(Constans.EXIST_CODE);
                httpResponseEntity.setMessage(Constans.COPY_EXIT_UPDATE_MESSAGE);
            }
        } catch (Exception e) {
            logger.info("updateQuestionnaireInfo 编辑问卷>>>>>>>>>>>" + e.getLocalizedMessage());
            httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
            httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
        }
        return httpResponseEntity;
    }

    /**
     * 删除题目
     */
    @RequestMapping(value = "/deleteQuestion", method = RequestMethod.POST,
            headers = "Accept=application/json")
    public HttpResponseEntity deleteQuestion(@RequestBody Map<String, Object> map) {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        int status = questionInfoService.deleteQuestion(map);
        try {
            if (status == 1) {
                httpResponseEntity.setCode(Constans.SUCCESS_CODE);
                httpResponseEntity.setMessage(Constans.DELETE_MESSAGE);
            } else {
                httpResponseEntity.setCode(Constans.EXIST_CODE);
                httpResponseEntity.setMessage(Constans.COPY_EXIT_UPDATE_MESSAGE);
            }
        } catch (Exception e) {
            logger.info("deleteQuestionnaireInfo 删除问卷>>>>>>>>>>>" + e.getLocalizedMessage());
            httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
            httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
        }
        return httpResponseEntity;
    }

    /**
     * 根据题目id查询题目
     */
    @RequestMapping(value = "/getQuestionById", method = RequestMethod.POST,
            headers = "Accept=application/json")
    public HttpResponseEntity getQuestionById(@RequestBody Map<String, Object> map) {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try {
            httpResponseEntity.setCode(Constans.SUCCESS_CODE);
            httpResponseEntity.setMessage(Constans.STATUS_MESSAGE);
            httpResponseEntity.setData(questionInfoService.getQuestionByIdList(map));
        } catch (Exception e) {
            logger.info("getQuestionById 根据题目id查询题目>>>>>>>>>>>" + e.getLocalizedMessage());
            httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
            httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
        }
        return httpResponseEntity;
    }
}
